package com.wenwen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wenwen.entity.Demo;
import com.wenwen.service.DemoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/demo")
public class DemoController {
    private final DemoService demoService;

    public DemoController(DemoService demoService) {
        this.demoService = demoService;
    }

    @GetMapping("/page")
    public Page<Demo> getPage(@RequestParam(defaultValue = "1") int page,
                              @RequestParam(defaultValue = "10") int pageSize,
                              @RequestParam(required = false) Integer id,
                              @RequestParam(required = false) String str) {
        // 根据不同的 pageSize 值来设置每页显示的数据条数
        if (pageSize == 20 || pageSize == 30) {
            pageSize = pageSize / 2;  // 每页显示二十条或三十条时，将 pageSize 减半
        }

        Page<Demo> resultPage = new Page<>(page, pageSize);
        QueryWrapper<Demo> queryWrapper = new QueryWrapper<>();
        if (id != null) {
            queryWrapper.eq("id", id);
        }
        if (str != null) {
            queryWrapper.like("str", str);
        }
        return demoService.page(resultPage, queryWrapper);
    }
}

